package com.ziyunying.weather.ui.screen

import android.Manifest

import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.ziyunying.weather.ui.screen.components.ActionBar
import com.ziyunying.weather.ui.screen.components.AirQuality
import com.ziyunying.weather.ui.screen.components.DailyForecast
import com.ziyunying.weather.ui.screen.components.PermissionDialog
import com.ziyunying.weather.ui.screen.components.WeeklyForecast
import com.ziyunying.weather.ui.theme.ColorBackground

@Composable
fun WeatherScreen(viewModel: WeatherViewModel = hiltViewModel()) {
    val weatherDataNow by viewModel.weatherDataNow.observeAsState()
    val showPermissionDialog by viewModel.showPermissionDialog.collectAsState()
    val currentCity by viewModel.currentCity.collectAsState()
    val forecastItems by viewModel.forecastItems.collectAsState()

    Scaffold(
        modifier = Modifier.fillMaxSize(),
        containerColor = ColorBackground
    ) { paddings ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .verticalScroll(rememberScrollState())
                .padding(paddings)
                .padding(horizontal = 24.dp, vertical = 10.dp)
        ) {
            ActionBar(location = currentCity)
            Spacer(
                modifier = Modifier.height(12.dp)
            )
            DailyForecast(
                weatherNow = weatherDataNow?.now,
            )
            Spacer(
                modifier = Modifier.height(24.dp)
            )
            AirQuality(
                weatherNow = weatherDataNow?.now,
            )
            Spacer(
                modifier = Modifier.height(24.dp)
            )
            WeeklyForecast(
                data = forecastItems
            )
        }

        val permissionLauncher = rememberLauncherForActivityResult(
            contract = ActivityResultContracts.RequestMultiplePermissions()
        ) { permissions ->
            val allGranted = permissions.entries.all { it.value }
            if (allGranted) {
                viewModel.onPermissionGranted()
            } else {
                viewModel.onPermissionDenied()
            }
        }

        // 使用封装的权限对话框组件
        if (showPermissionDialog) {
            PermissionDialog(
                onDismiss = { viewModel.onPermissionDenied() },
                onGrantPermission = {  // 请求系统权限
                    permissionLauncher.launch(
                        arrayOf(
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.ACCESS_COARSE_LOCATION
                        )
                    )
                }
            )
        }
    }
}
